package com.dhy.controller;

import com.dhy.mapper.BaseMapper;
import com.dhy.pojo.BasePojo;
import com.dhy.pojo.Department;
import com.dhy.query.BaseQuery;
import com.dhy.query.DepartmentQuery;
import com.dhy.service.IBaseService;
import com.dhy.service.imp.BaseServiceImpl;
import com.dhy.utils.AjaxResult;
import com.dhy.utils.annotation.MyPermissionAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class BaseController<T extends BasePojo> {

    @Autowired
    IBaseService<T> iBaseService;

    @PostMapping
    public AjaxResult insertOrUpdate(@RequestBody T t) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            if (t.getId() == null) {
                //执行修改方法
                iBaseService.insert(t);
                ajaxResult.setMessage("操作成功").setSuccess(true);
            } else {
                //执行增加方法
                iBaseService.update(t);
                ajaxResult.setMessage("操作成功").setSuccess(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setMessage("操作失败").setSuccess(false);
        }
        return ajaxResult;
    }

    //删除接口
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            iBaseService.deleteById(id);
            ajaxResult.setMessage("操作成功").setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setMessage("操作失败").setSuccess(false);
        }
        return ajaxResult;
    }

    @GetMapping("/{id}")
    public AjaxResult selectById( @PathVariable("id") Long id) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            T t = iBaseService.selectById(id);
            if (t == null) {
                ajaxResult.setMessage("操作失败").setSuccess(false);
            } else {
                ajaxResult.setMessage("操作成功").setSuccess(true).setData(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setMessage("操作失败").setSuccess(false);
        }
        return ajaxResult;
    }

    @GetMapping
    public AjaxResult selectAll() {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            List<T> t = iBaseService.selectAll();
            return ajaxResult.setMessage("操作成功").setSuccess(true).setData(t);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setSuccess(false).setMessage("操作失败");
        }
    }

    @GetMapping("/selectPageSize")
    public AjaxResult selectPageSize() {
        System.out.println("开始方法");
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            Long pageSize = iBaseService.selectPageSize();
            return ajaxResult.setMessage("操作成功").setSuccess(true).setData(pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping("/limitSelectAll")
    public AjaxResult selectLimit(@RequestBody BaseQuery BaseQuery) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            List<T> ts = iBaseService.selectLimit(BaseQuery);
            return ajaxResult.setMessage("操作成功").setSuccess(true).setData(ts);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping("/deleteByIds")
    public AjaxResult deleteByIds(@RequestBody List<Long> lists){
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            Long result = iBaseService.deleteByIds(lists);
            return ajaxResult.setMessage("操作成功").setSuccess(true).setData(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setSuccess(false).setMessage("操作失败");
        }
    }
}
